package com.sunl.mall.ali.mq.consumer;

import com.aliyun.openservices.shade.com.alibaba.fastjson.JSONObject;
import com.sunl.mall.ali.mq.model.RocketMqMessageBody;
import com.sunl.mall.ali.mq.model.RocketMqMessageContext;
import com.sunl.mall.common.api.util.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public interface SubscriptionLogger {
    // public static final log
    Logger log = LoggerFactory.getLogger(SubscriptionLogger.class);

    /**
     * 统一打印消费者的消息体解析结果日志
     *
     * @param context
     * @param consumerGroupId
     * @param body
     * @param <T>
     * @param <Context>
     */
    default <T, Context extends RocketMqMessageContext> void logDeserializeResult(Context context, String consumerGroupId, RocketMqMessageBody<T> body) {
        if (body == null) {
            Object[] params = {context.getMsgId(), context.getUniqMsgId(), context.getTopic(), context.getTag(), context.getKey(), consumerGroupId, context.getReconsumeTimes()};
            log.error("Mq consume Exception: record is null. msgId:[{}], uniqueMsgId:[{}], topic[{}], tag[{}], key[{}], consumerGroupId:[{}], retryTimes:[{}]", params);
        } else if (log.isDebugEnabled()) {
            Object[] params = {context.getMsgId(), context.getUniqMsgId(), context.getTopic(), context.getTag(), context.getKey(), consumerGroupId, context.getReconsumeTimes(), JSONObject.toJSONString(body)};
            log.debug("Mq Received message: msgId:[{}], uniqueMsgId:[{}], topic[{}], tag[{}], key[{}], consumerGroupId:[{}], retryTimes:[{}], record:[{}]", params);
        } else {
            Object[] params = {context.getMsgId(), context.getUniqMsgId(), context.getTopic(), context.getTag(), context.getKey(), consumerGroupId, context.getReconsumeTimes()};
            log.info("Mq Received message: msgId:[{}], uniqueMsgId:[{}], topic[{}], tag[{}], key[{}], consumerGroupId:[{}], retryTimes:[{}]", params);
        }
    }

    /**
     * 统一打印消费异常日志
     *
     * @param context
     * @param consumerGroupId
     * @param body
     * @param throwable
     * @param <T>
     * @param <Context>
     */
    default <T, Context extends RocketMqMessageContext> void logThrowable(Context context, String consumerGroupId, RocketMqMessageBody<T> body, Throwable throwable) {
        if (body != null) {
            Object[] params = {context.getMsgId(), context.getUniqMsgId(), context.getTopic(), context.getTag(), context.getKey(), consumerGroupId, context.getReconsumeTimes(), JSONObject.toJSONString(body), ExceptionUtil.getAsString(throwable)};
            log.error("Mq consume message Exception: msgId:[{}], uniqueMsgId:[{}], topic[{}], tag[{}], key[{}], consumerGroupId:[{}], retryTimes:[{}], record:[{}], Exception:{}", params);
        } else {
            Object[] params = {context.getMsgId(), context.getUniqMsgId(), context.getTopic(), context.getTag(), context.getKey(), consumerGroupId, context.getReconsumeTimes(), ExceptionUtil.getAsString(throwable)};
            log.error("Mq consume message Exception: msgId:[{}], uniqueMsgId:[{}], topic[{}], tag[{}], key[{}], consumerGroupId:[{}], retryTimes:[{}], record is null, Exception:{}", params);
        }
    }
}
